package com.jintoufs.service.scene.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.scene.ScenePriceMapper;
import com.jintoufs.dao.scene.SceneTicketMapper;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.images.Placard;
import com.jintoufs.domain.scene.SceneTicket;
import com.jintoufs.logAnnotation.SystemServiceLog;
import com.jintoufs.service.image.PlacardService;
import com.jintoufs.service.scene.SceneTicketService;
import com.jintoufs.util.valid.ValidUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SceneTicketServiceImpl implements SceneTicketService {


    @Resource
    private SceneTicketMapper sceneTicketMapper;

    @Resource
    private ScenePriceMapper scenePriceMapper;

    @Resource
    private PlacardService placardService;

    @Override
    @SystemServiceLog(description = "删除房间")
    public void delete(String id) {
        String[] ids = id.split(",");
        for (String idm:ids){
            SceneTicket sceneTicket = sceneTicketMapper.selectByPrimaryKey(idm);
            sceneTicket.setStatus("0");
            sceneTicketMapper.updateByPrimaryKeySelective(sceneTicket);
//            sceneTicketMapper.deleteByPrimaryKey(idm);
        }
    }

    @Override
    @SystemServiceLog(description = "修改房间信息")
    public void update(SceneTicket hotel,  Customer customer) {
        sceneTicketMapper.updateByPrimaryKeySelective(hotel);
        updateTicketPlacard(hotel,customer);
    }

    @Override
    @SystemServiceLog(description = "新增房间")
    public SceneTicket add(SceneTicket hotel,Customer customer) {
        sceneTicketMapper.insert(hotel);
        updateTicketPlacard(hotel,customer);
        return hotel;

    }

    @Override
    public List<Map<String,Object>> queryByParams(Map<String, Object> params) {
        return sceneTicketMapper.queryByParams(params);
    }

    @Override
    public List<Map<String, Object>> selectByParams(Map<String, Object> params) {
        return sceneTicketMapper.selectByParams(params);
    }
    @Override
    public List<Map<String,Object>> getByFilter(QueryFilters filters) {
        return sceneTicketMapper.getByFilter(filters);
    }

    @Override
    public SceneTicket queryById(String id) {
        return sceneTicketMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer getMinReserve(String id, String startDate) {
        Map<String,Object> params = new HashMap<>();
        params.put("id",id);
        params.put("startDate",startDate);
        try {
            return scenePriceMapper.getMinReserve(params);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    void updateTicketPlacard(SceneTicket sceneTicket,Customer customer){
        HashMap<String, Object> map = new HashMap();
        map.put("images", sceneTicket.getPlacard());
        List<Placard> list = placardService.selectByparams(map);
        if(ValidUtil.isEmpty(list)){
            Placard placard=new Placard();
            placard.setTitle(sceneTicket.getTicketName());
            placard.setImages(sceneTicket.getPlacard());
            placard.setCustomerId(customer.getId());
            placardService.insert(placard);
        }
    }
}
